package net.duohuo.dhroiddemos.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.innosystec.unrar.Archive;
import de.innosystec.unrar.exception.RarException;
import de.innosystec.unrar.rarfile.FileHeader;

/**
 * 类说明
 * 
 * @creator WLC 497937995@qq.com
 * @create-time 2013-5-13 上午1:22:21
 * @revision 1.0
 */
public class Unrar {
	private static String fileName = "";
	private static String needSuffix = ".txt";
	private static long extFileSize = 5120; // 解压大于5120bit的文件

	public static void unrarFilesThread(final String rarFileName, final String extPlace, final String extName) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				unrarFilesThread(rarFileName, extPlace, extName);
			}
		}).start();
	}

	/**
	 * 对rar文件解压
	 * 
	 * @param rarFileName
	 *            需要解压的文件
	 * @param extPlace
	 *            解压后的路径
	 * @return
	 */
	public static boolean unrarFiles(String rarFileName, String extPlace, String extName) {
		boolean flag = false;
		Archive archive = null;
		File out = null;
		File file = null;
		File dir = null;
		FileOutputStream os = null;
		FileHeader fh = null;
		String path, dirPath = "";

		if (!new File(extPlace).isDirectory())
			new File(extPlace).mkdirs();

		try {
			file = new File(rarFileName);
			System.out.println("rarFileName=" + rarFileName);
			System.out.println("file=" + file);
			archive = new Archive(file);
		} catch (RarException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			if (file != null) {
				file = null;
			}
		}
		if (archive != null) {
			try {
				fh = archive.nextFileHeader();
				while (fh != null) {
					// Unicode字符转码，解决中文问题
					if (fh.isUnicode()) {
						System.out.println("Is Unicode");
						fileName = fh.getFileNameW().trim();
					} else {
						System.out.println("Is not Unicode");
						fileName = fh.getFileNameString().trim();
					}
					path = (extPlace + "/" + fileName).replaceAll("\\\\", "/");
					int end = path.lastIndexOf("/");
					if (end != -1) {
						dirPath = path.substring(0, end);
					}
					try {
						dir = new File(dirPath);
						if (!dir.exists()) {
							dir.mkdirs();
						}
					} catch (RuntimeException e1) {
						e1.printStackTrace();
					} finally {
						if (dir != null) {
							dir = null;
						}
					}
					if (fh.isDirectory()) {
						fh = archive.nextFileHeader();
						continue;
					}
					String subPath = path.substring(path.lastIndexOf("."));
					if (subPath.equals(needSuffix)) {
						if (fh.getUnpSize() > extFileSize) {
							// System.out.println("大于10000Byte文件解压===》");
							path = (extPlace + extName + needSuffix).replaceAll("\\\\", "/");
							out = new File(path);
							try {
								if (out.exists()) {
									System.out.println("文件已经存在，删除。。");
									out.delete();
								}
								os = new FileOutputStream(out);
								archive.extractFile(fh, os);
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							} catch (RarException e) {
								e.printStackTrace();
							} finally {
								if (os != null) {
									try {
										os.close();
									} catch (IOException e) {
										e.printStackTrace();
									}
								}
								if (out != null) {
									out = null;
								}
							}
						}
					}
					fh = archive.nextFileHeader();
				}
			} catch (RuntimeException e) {
				e.printStackTrace();
			} finally {
				fh = null;
				if (archive != null) {
					try {
						archive.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			flag = true;
		}
		return flag;
	}

	/**
	 * @param str
	 * @return 对中文路径进行修正
	 */
	public static boolean existZH(String str) {
		String regEx = "[\\u4e00-\\u9fa5]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		while (m.find()) {
			return true;
		}
		return false;
	}
}